// 增强版鼠标跟随生成艺术 - 基于原始压缩代码的Processing版本
// 原始代码: a=(y,d=mag(k=(y<11?6+sin(y^8)*6:y/5+cos(y/2))*cos(i-t/4),e=y/7-13)+sin(e/4+t)/2)=>point((q=y*k/d*(3+sin(d*2+y/2-t*4)))+60*cos(c=d/2+1-t/2)+200,q*sin(c)+d*29-170)
// t=0,draw=$=>{t||createCanvas(w=400,w);background(9).stroke(w,96);for(t+=PI/120,i=1e4;i--;)a(i/345)}

float t = 0;
float w = 400;

// 鼠标位置变量
float mouseTargetX = 200;
float mouseTargetY = 200;
float currentMouseX = 200;
float currentMouseY = 200;

// 颜色模式
int colorMode = 0;
float hueOffset = 0;

// 性能优化
int pointCount = 5000;
float trailAlpha = 15;

// 交互参数
float speedMultiplier = 1.0;
float scaleMultiplier = 1.0;
float complexity = 1.0;

void setup() {
  size(800, 600, P2D);
  background(9);
  stroke(255, 96);
  strokeWeight(1);
  noFill();
  
  // 初始化鼠标位置到画布中心
  mouseTargetX = width / 2;
  mouseTargetY = height / 2;
  currentMouseX = mouseTargetX;
  currentMouseY = mouseTargetY;
  
  // 平滑渲染
  smooth(8);
}

void draw() {
  // 半透明背景实现拖尾效果
  fill(9, trailAlpha);
  noStroke();
  rect(0, 0, width, height);
  
  // 设置颜色
  updateColor();
  strokeWeight(1);
  
  // 平滑鼠标跟随
  currentMouseX = lerp(currentMouseX, mouseTargetX, 0.1);
  currentMouseY = lerp(currentMouseY, mouseTargetY, 0.1);
  
  // 更新时间
  t += (PI / 120) * speedMultiplier;
  hueOffset += 0.5;
  
  // 绘制生成艺术图案
  for (int i = 0; i < pointCount; i++) {
    drawArtPoint(i / 345.0);
  }
  
  // 绘制鼠标跟随指示器
  drawMouseIndicator();
  
  // 显示说明文本
  drawInstructions();
}

// 更新颜色设置
void updateColor() {
  switch(colorMode) {
    case 0: // 白色
      stroke(255, 96);
      break;
    case 1: // 彩虹色
      colorMode(HSB, 360, 100, 100);
      stroke((hueOffset + t * 50) % 360, 80, 100, 96);
      colorMode(RGB, 255);
      break;
    case 2: // 蓝色调
      stroke(100, 150, 255, 96);
      break;
    case 3: // 暖色调
      stroke(255, 150, 100, 96);
      break;
  }
}

// 原始函数的Processing版本，添加鼠标跟随和增强功能
void drawArtPoint(float y) {
  // 应用复杂度参数
  y *= complexity;
  
  // 计算k值
  float k;
  if (y < 11) {
    k = (6 + sin(pow(y, 8)) * 6) * cos(t - t/4);
  } else {
    k = (y/5 + cos(y/2)) * cos(t - t/4);
  }
  
  // 计算e值
  float e = y/7 - 13;
  
  // 计算d值 (距离)
  float d = mag(k, e) + sin(e/4 + t)/2;
  
  // 计算c值
  float c = d/2 + 1 - t/2;
  
  // 计算q值
  float q = y * k / d * (3 + sin(d*2 + y/2 - t*4));
  
  // 应用缩放参数
  q *= scaleMultiplier;
  
  // 计算点的位置 - 使用鼠标位置作为中心
  float x = q + 60 * cos(c) + currentMouseX;
  float yPos = q * sin(c) + d * 29 - 170 + currentMouseY;
  
  // 绘制点
  point(x, yPos);
}

// 绘制鼠标跟随指示器
void drawMouseIndicator() {
  pushStyle();
  noFill();
  stroke(255, 50);
  strokeWeight(1);
  
  // 绘制十字准线
  line(currentMouseX - 20, currentMouseY, currentMouseX + 20, currentMouseY);
  line(currentMouseX, currentMouseY - 20, currentMouseX, currentMouseY + 20);
  
  // 绘制圆圈
  ellipse(currentMouseX, currentMouseY, 30, 30);
  popStyle();
}

// 鼠标移动事件
void mouseMoved() {
  mouseTargetX = mouseX;
  mouseTargetY = mouseY;
}

// 鼠标拖拽事件
void mouseDragged() {
  mouseTargetX = mouseX;
  mouseTargetY = mouseY;
}

// 键盘控制
void keyPressed() {
  if (key == 'r' || key == 'R') {
    // 重置画布
    background(9);
  } else if (key == 'c' || key == 'C') {
    // 清除画布并重置参数
    background(9);
    t = 0;
    speedMultiplier = 1.0;
    scaleMultiplier = 1.0;
    complexity = 1.0;
    pointCount = 5000;
    trailAlpha = 15;
  } else if (key == 's' || key == 'S') {
    // 保存图片
    save("mouse_following_art_" + year() + month() + day() + "_" + hour() + minute() + second() + ".png");
  } else if (key == '1') {
    // 白色模式
    colorMode = 0;
  } else if (key == '2') {
    // 彩虹模式
    colorMode = 1;
  } else if (key == '3') {
    // 蓝色模式
    colorMode = 2;
  } else if (key == '4') {
    // 暖色模式
    colorMode = 3;
  } else if (key == '+' || key == '=') {
    // 增加速度
    speedMultiplier = min(speedMultiplier + 0.2, 5.0);
  } else if (key == '-' || key == '_') {
    // 减少速度
    speedMultiplier = max(speedMultiplier - 0.2, 0.1);
  } else if (key == '[') {
    // 减少点数
    pointCount = max(pointCount - 1000, 1000);
  } else if (key == ']') {
    // 增加点数
    pointCount = min(pointCount + 1000, 20000);
  } else if (key == ',') {
    // 减少拖尾
    trailAlpha = max(trailAlpha - 5, 5);
  } else if (key == '.') {
    // 增加拖尾
    trailAlpha = min(trailAlpha + 5, 50);
  }
}

// 鼠标滚轮控制缩放
void mouseWheel(MouseEvent event) {
  float e = event.getCount();
  if (e < 0) {
    scaleMultiplier = min(scaleMultiplier + 0.1, 3.0);
  } else {
    scaleMultiplier = max(scaleMultiplier - 0.1, 0.1);
  }
}

// 显示说明文本
void drawInstructions() {
  pushStyle();
  fill(255, 150);
  noStroke();
  textAlign(LEFT);
  textSize(12);
  
  String infoText = "移动鼠标: 控制图案中心 | ";
  infoText += "滚轮: 缩放 | ";
  infoText += "R: 重置 | C: 清除 | S: 保存";
  text(infoText, 10, height - 40);
  
  String colorText = "颜色: 1-白色 2-彩虹 3-蓝色 4-暖色 | ";
  colorText += "速度: +/- | ";
  colorText += "点数: [] | ";
  colorText += "拖尾: ,.";
  text(colorText, 10, height - 20);
  
  // 显示当前参数
  textAlign(RIGHT);
  String paramsText = String.format("速度: %.1f | 缩放: %.1f | 点数: %d | 拖尾: %d", 
    speedMultiplier, scaleMultiplier, pointCount, trailAlpha);
  text(paramsText, width - 10, height - 20);
  
  popStyle();
}